package com.ruoyi.project.order.controller;


import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.constant.OrderCheckStatu;
import com.ruoyi.common.constant.OrderStatu;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.order.domain.Customer;
import com.ruoyi.project.order.domain.WorkOrder;
import com.ruoyi.project.order.mapper.WorkOrderMapper;
import com.ruoyi.project.order.service.ICustomerService;
import com.ruoyi.project.order.service.IOrderTypeService;
import com.ruoyi.project.order.service.IWorkOrderService;
import com.ruoyi.project.order.vo.OrderListVo;
import com.ruoyi.project.order.vo.SelectOrderVo;
import com.ruoyi.project.order.wx.TemplateId;
import com.ruoyi.project.order.wx.WxSender;
import com.ruoyi.project.system.user.service.IUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author L
 * @since 2020-08-07
 */
@Controller
@RequestMapping("/check")
@Slf4j
public class OrderCheckController extends BaseController {
    @Autowired
    ICustomerService iCustomerService;
    @Autowired
    IWorkOrderService iWorkOrderService;
    @Autowired
    IOrderTypeService iOrderTypeService;
    @Autowired
    IUserService iUserService;
    @Autowired
    WorkOrderMapper workOrderMapper;


    @GetMapping()
    public String orderMy(Model model) {
        return  "order/order_check";
    }

    @RequestMapping("/list")
    @ResponseBody
    public TableDataInfo list(SelectOrderVo selectOrderVo) {
        startPage();
        //判断身份
        selectOrderVo.setOrderStatu(OrderStatu.YiWanCheng.getValue());
        List<OrderListVo> orderListVos = workOrderMapper.LGetListNoContent(selectOrderVo);
        return getDataTable(orderListVos);
    }

    @RequestMapping("shenheOk/{id}")
    @ResponseBody
    public AjaxResult finish(@PathVariable("id") Integer Id) {
        WorkOrder rWorkOrder = iWorkOrderService.getById(Id);
        if (rWorkOrder.getOrderStatus()== OrderStatu.YiWanCheng.getValue()) {
            //设置审核成功
            rWorkOrder.setCheckStatus(OrderCheckStatu.checkSuccess.getValue());
            //设置审核人
            rWorkOrder.setCheckerId(getUserId().intValue());
            //加入完成时间
            iWorkOrderService.updateById(rWorkOrder);
            WxSender.sendMsg(rWorkOrder.getWorkerId(), TemplateId.WorkOrderCheck, "你的工单: " + rWorkOrder.getOrderTitle(), rWorkOrder.getOrderId(), DateUtil.now(), "任务成功", "恭喜你哦,请继续加油");

            return AjaxResult.success("审核成功");
        }
        return AjaxResult.error("该订单当前状态为: " + OrderStatu.getByValue(rWorkOrder.getOrderStatus()).getName() + ",无法审核");
    }
    @RequestMapping("shenheNo/{id}")
    @ResponseBody
    public AjaxResult shenheNo(@PathVariable("id") Integer Id) {
        WorkOrder rWorkOrder = iWorkOrderService.getById(Id);
        if (rWorkOrder.getOrderStatus()== OrderStatu.YiWanCheng.getValue()) {
            //设置审核成功
            rWorkOrder.setCheckStatus(OrderCheckStatu.checkFail.getValue());
            //设置审核人
            rWorkOrder.setCheckerId(getUserId().intValue());
            //加入完成时间
            iWorkOrderService.updateById(rWorkOrder);
            //通知
            WxSender.sendMsg(rWorkOrder.getWorkerId(), TemplateId.WorkOrderCheck, "你的工单: " + rWorkOrder.getOrderTitle(), rWorkOrder.getOrderId(), DateUtil.now(), "任务失败", "不要灰心,下次继续努力哦");

            return AjaxResult.success("审核成功");
        }
        return AjaxResult.error("该订单当前状态为: " + OrderStatu.getByValue(rWorkOrder.getOrderStatus()).getName() + ",无法审核");
    }
    @RequestMapping("comment")
    @ResponseBody
    public AjaxResult shenheNo(  Integer id , String comment) {
        WorkOrder rWorkOrder = iWorkOrderService.getById(id);
        if (rWorkOrder.getCheckStatus()!= OrderCheckStatu.checkNo.getValue()) {
            rWorkOrder.setOrderComment(comment);
            iWorkOrderService.updateById(rWorkOrder);
            //通知
            WxSender.sendMsg(rWorkOrder.getWorkerId(), TemplateId.WorkOrderComment, "你的订单已被评价", rWorkOrder.getOrderId(), "请到系统查看", DateUtil.formatLocalDateTime(rWorkOrder.getOrderFinishTime()), "评价内容"+(comment.length()>25?"内容过长请到系统查看":comment));
            return AjaxResult.success("评价成功");
        }
        return AjaxResult.error("该订单当前状态为: " + OrderCheckStatu.getByValue(rWorkOrder.getCheckStatus()).getName() + "请审核后评价");
    }


}
